/**
 * @author VernHe
 * @date 2021年04月20日 17:52
 *
 * 示例 1：
 *
 * 输入：s = "abc", t = "ahbgdc"
 * 输出：true
 */
public class Solution_0392 {
    public boolean isSubsequence(String s, String t) {
        char[] char1 = s.toCharArray();
        char[] char2 = t.toCharArray();
        int length1 = char1.length;
        if (length1 == 0) {
            return true;
        }
        int length2 = char2.length;
        if (length2 < length1) {
            return false;
        }

        // dp[i][j]表示s字符串的前i个字符是否是t字符串的前j个字符的字串
        boolean[][] dp = new boolean[length1 + 1][ length2 + 1];
        // 初始化
        dp[0][0] = true;
        for (int i = 0; i < length2; i++) {
            if (char1[0] == char2[i]) {
                dp[0][i] = true;
            }
        }

        for (int i = 1; i <= length1; i++) {
            for (int j = 1; j <= length2; j++) {
                if (char1[i-1] == char2[j-1]) {
                    dp[i][j] = dp[i-1][j-1];
                } else {
                    dp[i][j] = dp[i][j-1];
                }
            }
        }
        return dp[length1][length2];
    }
}
